<template>
	<view class="login">
		<!-- <view style="height: 0; width: 100%; background: #bad8f2">
			<view class="status_bar">
				<view @tap="goHome">首页</view>
			</view>
		</view> -->
		<view class="logo_bkg">
			<view class="logo_content">
				<text class="login_title">理性购物 好物尽享</text>
			</view>
		</view>
		<view class="login_top">
			<view class="namePhone">
				<view class="name">
					<view class="name_phone">
						<input style="font-size: 28rpx" v-model="phone" type="number" placeholder="请输入手机号码" />
					</view>
				</view>
				<view class="phone">
					<view class="phone_number">
						<input style="font-size: 28rpx" v-model="yzm" type="number" maxlength="6"
							placeholder="请输入验证码" />
						<text :style="{
                                backgroundColor: themeColor
                            }" class="yam_num" @click="get_code">
							{{ yzm_text }}
						</text>
					</view>
				</view>
				<text class="tips">未绑定的手机号验证后将自动绑定</text>
			</view>

			<button class="sub_login" :style="{
                    backgroundColor: themeColor
                }" @click="sub_login">
				确认绑定
			</button>
		</view>
	</view>
</template>
<script>
	import {
		request,
		getZWZUserDetail,
		checkHasUserBasicInfo,
		checkHasShiMingRenZheng,
		saveUserId,
		saveZWZUserDetail,
		getUserId
	} from '@/utils';
	import {
		baseUrl
	} from '@/utils/constant';
	export default {
		data() {
			return {
				userId: '',
				yzm_text: '获取验证码',
				yzm_text_ing: false,
				yzm_daojishi: 0,
				web_view: false,
				check_stu: false,
				phone: '',
				yzm: '',
				userDetail: '',
				codeKey: '',
				codeTime: '',
				appLogo: '',
				appName: ''
			};
		},
		onLoad() {
			this.userId = getUserId()
		},

		onShow() {},
		methods: {
			get_code() {
				var that = this;
				var myreg = /^(((1[0-9]{2}))+\d{8})$/;
				if (!myreg.test(this.phone)) {
					uni.showToast({
						icon: 'none',
						title: '手机号格式错误'
					});
					return false;
				}
				if (that.yzm_text_ing === true) {
					//倒计时中
					return false;
				}
				that.yzm_text_ing = true;
				that.yzm_daojishi = 120; //倒计时30秒
				that.yzm_text = that.yzm_daojishi + 'S';
				request(
					'/member/mall/aliPay/user/sendSmsCode', {
						mobile: that.phone
					},
					'GET',
					false
				).then((res) => {
					console.log('发送短信====', res);
					that.codeKey = res.data.codeKey;
					that.codeTime = res.data.codeTime;
				});

				that.setInterval = setInterval(function() {
					that.yzm_daojishi = that.yzm_daojishi - 1; //时间减少
					that.yzm_text = that.yzm_daojishi + 'S';

					if (that.yzm_daojishi <= 0) {
						that.yzm_text = '重新获取';
						that.yzm_daojishi = 0;
						that.yzm_text_ing = false;
						clearInterval(that.setInterval); //
					}
				}, 1000);
			},
			sub_login(e) {
				if (this.phone == '13991373115') {
					uni.showToast({
						icon: 'none',
						title: '登录出错'
					});
					return
				}
				uni.showLoading({
					title: '拼命加载中'
				});
				var that = this;
				var myreg = /^(((1[0-9]{2}))+\d{8})$/;
				if (!myreg.test(this.phone)) {
					uni.showToast({
						icon: 'none',
						title: '手机号输入错误'
					});
					return false;
				}
				if (this.yzm.length != 6) {
					uni.showToast({
						icon: 'none',
						title: '验证码错误'
					});
					return false;
				}
				var postData = {};
				postData.phone = this.phone;
				postData.yzm = this.yzm;
				request(
					'member/mall/aliPay/user/bindMobileToApp', {
						codeKey: that.codeKey,
						codeTime: that.codeTime,
						smsCode: that.yzm,
						telephone: that.phone,
						uid: this.userId
					},
					'POST',
					false
				).then((res) => {
					console.log('新获取手机号接口的返回数据', res);
					if (res.code && res.code == '500') {
						//登陆失败
						uni.showToast({
							icon: 'none',
							title: res.msg
						});
						return false;
					}
					if (res.data.errorCode == '-1') {
						uni.showToast({
							icon: 'none',
							title: res.data.errorMessage
						});
						return false;
					}
					that.userDetail = res.data;
					if (that.userDetail.uid) {
						saveUserId(that.userDetail.uid);
					}
					saveZWZUserDetail(that.userDetail);
					uni.hideLoading();
					uni.showToast({
						title: '绑定手机成功,请返回浏览器继续下单签约操作!',
						icon: 'none',
						duration: 2500
					});
					let timer1 = null
					timer1 = setTimeout(() => {
						uni.navigateBack({
							delta: 1
						});
						timer1 = null
					}, 2500); // 延迟0.5秒
				});
			},
			goHome() {
				uni.switchTab({
					url: '/pages/index/index'
				});
			},
		}
	};
</script>

<style lang="less" scoped>
	uni-app {
		margin: 0;
		padding: 0;
		background-color: #fff;
	}

	.login-text {
		font-size: 44rpx;
		color: black;
		font-weight: 700;
	}

	.logo_bkg {
		// min-height: 300rpx;
		position: relative;
		height: auto;

		.logo_content {
			text-align: center;
			margin: 10vh auto auto;
			left: 0;
			right: 0;
			font-size: 38rpx;
			font-family: 'MicrosoftYaHei';
			color: #fff;
			font-weight: bold;

			.login_title {
				display: block;
				font-size: 28rpx;
				font-weight: bold;
				color: #ca9e64;
			}
		}
	}

	.login_top {
		display: flex;
		flex-direction: column;
		justify-content: center;
		box-sizing: border-box;
		align-items: center;
		padding: 0 20rpx 100rpx 20rpx;
		background-color: #fff;
		overflow: hidden;

		.namePhone {
			box-sizing: border-box;
			padding: 0 60rpx 20rpx;
			margin-top: 50rpx;
			border-radius: 20rpx;
			display: flex;
			flex-direction: column;
			width: 100%;

			/deep/.input-placeholder {
				font-size: 24rpx;
				color: #c5c5c5;
			}

			.icon1 {
				width: 22rpx;
				height: 30rpx;
				align-self: flex-start;
				margin-right: 8rpx;
			}

			.name_icon {
				display: flex;
				align-items: center;
				color: #666;
				line-height: 28rpx;
				margin-bottom: 25rpx;
				font-size: 26rpx;
			}

			.name_phone {
				display: flex;
				align-items: center;

				.text {
					display: inline-block;
					padding-right: 15rpx;
					margin-right: 15rpx;
					border-right: 1px solid #999;
					height: 28rpx;
					line-height: 28rpx;
				}
			}

			.name {
				display: flex;
				flex-direction: column;
				padding: 15rpx 0;
				border-bottom: 1px solid #e0e0e0;
			}

			.phone {
				display: flex;
				flex-direction: column;
				padding-bottom: 15rpx;
				border-bottom: 1px solid #e0e0e0;
				margin: 40rpx 0;

				.phone_number {
					display: flex;
					align-items: center;
				}

				.yam_num {
					font-size: 22rpx;
					line-height: 45rpx;
					height: 45rpx;
					text-align: center;
					color: #fff;
					border-radius: 40rpx;
					padding: 0 20rpx;
					margin-left: auto;
					margin-right: 20rpx;
					background-color: #be9364;
				}
			}

			.tips {
				text-align: center;
				font-size: 24rpx;
				margin: 10rpx 0;
				color: #999999;
			}

			.ysxy {
				font-size: 24rpx;
				text-align: center;
				color: #999999;
				padding: 20rpx 0;
				display: flex;
				justify-content: center;
				align-items: center;

				.b {
					color: #f0945b;
				}

				/deep/.u-checkbox__label {
					font-size: 24rpx;
					margin-right: 0;
					color: #999999;
				}

				/deep/.u-checkbox__icon-wrap {
					width: 26rpx !important;
					height: 26rpx !important;
				}
			}
		}

		.sub_login {
			width: 85%;
			color: #fff;
			font-weight: bold;
			letter-spacing: 0.05em;
			margin: 50rpx auto;
			font-size: 16px;
			border-radius: 30px;

			&::after {
				border: none;
			}

			background-color: #be9364;
		}
	}

	.status_bar {
		position: fixed;
		top: var(--status-bar-height);
		right: 20rpx;
		z-index: 999;
		font-size: 30rpx;
		font-weight: bold;
		font-family: 'Arial';
		padding: 20rpx;
		color: #282828;
		font-weight: bold;
	}

	.lifestyle {
		position: absolute;
		bottom: 30rpx;
		left: 50%;
		transform: translateX(-50%);
	}
</style>